package com.stu.manager.organization.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stu.manager.config.Result;
import com.stu.manager.handler.LoginHandler;
import com.stu.manager.organization.entity.SysClassinfo;
import com.stu.manager.organization.entity.SysRoom;
import com.stu.manager.organization.entity.dto.AddClassDto;
import com.stu.manager.organization.entity.dto.ClassInfoDto;
import com.stu.manager.organization.entity.dto.RoomDto;
import com.stu.manager.organization.service.SysProfessionalServiceImpl;
import com.stu.manager.organization.service.SysRoomServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-02-13
 */
@RestController
@RequestMapping("/api/room")
@CrossOrigin
public class SysRoomController {

    @Autowired
    private SysRoomServiceImpl roomService;
    @Autowired
    private LoginHandler loginHandler;
    @PostMapping("pageRoom/{current}/{limit}")
    @ApiOperation("模糊查询加分页展示班级")
    public Result pageRoom(@PathVariable Long current,
                                @PathVariable Long limit,
                                @RequestBody(required = false) RoomDto roomDto,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        Page<SysRoom> pageRoom = new Page<>(current, limit);
        QueryWrapper<SysRoom> wrapper = new QueryWrapper<>();
        String roomAddr = roomDto.getRoomAddr();
        if (!StringUtils.isEmpty(roomAddr)) {
            wrapper.like("room_addr", roomAddr);
        }

        IPage<SysRoom> sysRoomIPage = roomService.pageRoom(pageRoom, wrapper);
        List<SysRoom> records = sysRoomIPage.getRecords();
        long total = sysRoomIPage.getTotal();
        return Result.ok().data("total", total).data("records", records);
    }


    @PostMapping("addRoom")
    @ApiOperation("添加教室信息")
    public Result addRoom(@RequestBody RoomDto roomDto,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        Result result = roomService.addRoom(roomDto);
        return result;
    }


    @GetMapping("getRoomById/{id}")
    @ApiOperation("查询一个教室信息")
    public Result getRoomById(@PathVariable String id,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        SysRoom roomById = roomService.getRoomById(id);
        return Result.ok().data("item",roomById);
    }


    @PostMapping("updateRoom")
    @ApiOperation("修改教室信息")
    public Result updateRoom(@RequestBody SysRoom sysRoom,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        Result result = roomService.updateRoom(sysRoom);
        return result;
    }

    @DeleteMapping("delRoom/{id}")
    @ApiOperation("删除教室信息")
    public Result delRoom(@PathVariable String id,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        QueryWrapper<SysRoom> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        boolean remove = roomService.remove(wrapper);
        if (remove) {
            return Result.ok().message("删除成功！");
        } else {
            return Result.error().message("删除失败！");
        }
    }

    @GetMapping("getAllRoom")
    @ApiOperation("查询所有教室")
    public Result getAllRoom(HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        List<SysRoom> allRoom = roomService.getAllRoom();
        return Result.ok().data("rooms",allRoom);
    }

}

